/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2008 Rhythm Computing All rights reserved.
 * 
 * Messages.java	
 * @date: Oct 12, 2008
 * @author Michael J. Lee
 * 
 */

package com.rhythm.base;

import com.rhythm.commons.collections.Lists;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Michael J. Lee
 */
public class Messages extends ArrayList<Message> implements Serializable {

    private static final long serialVersionUID = 1;

       
    /**
     * Creates a new empty instance of <code>Messages</code>.
     * <p>
     * <strong>NOTE:</strong><br/>
     * Each instance of <code>Message</code> in the returned <code>Messages</code>
     * will have the default Id.
     * </p>
     * 
     * @return a new empty instance of <code>Messages</code>
     */
    public static Messages emtpyInstance() {
        return new Messages();

    }
    
    /**
     * Creates a new instance of <code>Messages</code> with the one give message
     * <p>
     * <strong>NOTE:</strong><br/>
     * Each instance of <code>Message</code> in the returned <code>Messages</code>
     * will have the default Id.
     * </p>
     * @param singleMessage a message to add
     * @return a new instance of <code>Messages</code> with the one give message
     */
    public static Messages of(String singleMessage) {
        return new Messages().append(singleMessage);

    }

    /**
     * Creates a new instance of <code>Messages</code> with all the give messages.
     * <p>
     * <strong>NOTE:</strong><br/>
     * Each instance of <code>Message</code> in the returned <code>Messages</code>
     * will have the default Id.
     * </p>
     * @param messages messages to add
     * @return  a new instance of <code>Messages</code> with all the give messages
     */
    public static Messages of(String... messages) {
        Messages newMessages = new Messages();
        for (String aMessage : messages) {
            newMessages.add(Message.of(aMessage));
        }
        return newMessages;

    }
    
    
    /**
     * Forces a <code>IllegalArgumentException</code> of the give message. 
     * 
     * @throws java.lang.IllegalArgumentException guaranteed
     */
    public void toIllegalArgumentException() throws IllegalArgumentException{
        StringBuilder exceptionMessage = new StringBuilder();
        for(Message aMessage : this)
        {
            exceptionMessage.append(aMessage.getMessage());
            
            if (!Lists.isLastItem(aMessage, this))
                 exceptionMessage.append(", ");
        }
        throw new IllegalArgumentException(exceptionMessage.toString());
    }
    
    /**
     * Returns if the current instance contains any <code>Message</code>
     * @return true if the current instance contains any <code>Message</code>, otherwise false.
     * @see isEmpty();
     */
    public boolean containsMessages(){
        return Booleans.inverse(isEmpty());
    }

    public Messages append(String message) {
        //TODO: javaDoc
        super.add(Message.of(message));
        return this;
    }
}
